package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 菜品模块
@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {

    //菜品表
    @Autowired
    private DishMapper dishMapper;

    //菜品口味表
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    //菜品分类表
    @Autowired
    private CategoryMapper categoryMapper;

    //分页查询
    @Override
    public Page<Dish> findBypage(Integer pageNum, Integer pageSize, String name) {
        // 构建菜品条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //糊涂工具包StrUtil
        qw.like(StrUtil.isNotBlank(name), Dish::getName, name);
        //开启分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //分页查询
        page = dishMapper.selectPage(page, qw);
        //遍历菜品列表
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            //根据菜品id查询口味列表
            LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
            dqw.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dqw);
            //封装口味列表
            dish.setFlavors(dishFlavorList);
            // 根据分类id查询分类信息
            Category category = categoryMapper.selectById(dish.getCategoryId());
            // 封装分类名称
            dish.setCategoryName(category.getName());
        }

        // 5.返回分页对象
        return page;
    }


    // 菜品管理=> 新增菜品
    @Override
    public void save(Dish dish) {
// 1.新增菜品基本信息
        log.info("新增之前：" + dish.getId());
        dishMapper.insert(dish);
        log.info("新增之后：" + dish.getId()); // 主键返回

        // 遍历口味
        for (DishFlavor flavor : dish.getFlavors()) {
            // 关联菜品id
            flavor.setDishId(dish.getId());
            // 新增口味
            dishFlavorMapper.insert(flavor);
        }
    }


    // 修改先回显菜品
    @Override
    public Dish findById(Long id) {
        //先根据菜品id查询
        Dish dish = dishMapper.selectById(id);
        //在根据菜品id查询口味 构建条件
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, id);
        //调用mapper
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw);
        // 将口味设置到菜品
        dish.setFlavors(dishFlavors);
        //返回菜品
        return dish;
    }


    // 菜品管理 => 修改菜品
    @Override
    public void updateById(Dish dish) {
        //修改菜品基本信息
        dishMapper.updateById(dish);
        //根据菜品id删除原来的口味列表
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(qw);
        //遍历新增口味
        for (DishFlavor flavor : dish.getFlavors()) {
            // 关联菜品id
            flavor.setDishId(dish.getId());
            // 口味新增
            dishFlavorMapper.insert(flavor);
        }
        //遍历新提交的口味列表

    }


    //菜品管理 =>删除/批量删除
    @Override
    public void deleteBatchIds(List<Long> ids) {
        //判断有启售的菜品不让删
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids);//in (1,3,5)
        qw.eq(Dish::getStatus, 1);
        //返回一列
        Integer count = dishMapper.selectCount(qw);
        if (count > 0) {
            throw new CustomException("您选择的菜品有起售状态,请重新选择");
        }

        dishMapper.deleteBatchIds(ids);
        //删口味
        LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
        qw2.in(DishFlavor::getDishId, ids);
        dishFlavorMapper.delete(qw2);
   /*   //调用mapper删除
        dishMapper.deleteBatchIds(ids);

        // 再删口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.in(DishFlavor::getDishId, ids);
        dishFlavorMapper.delete(qw);*/

    }

    //菜品启售 禁售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids);
        // 修改实体
        Dish dish = new Dish();
        dish.setStatus(status);
        // 调用mapper
        dishMapper.update(dish, qw);


    }


    // 根据分类查询菜品列表(回显)
    @Override
    public List<Dish> findListByCategoryId(Long categoryId) {
        //根据分类查询

            LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
            qw.eq(Dish::getCategoryId, categoryId);
            qw.eq(Dish::getStatus, 1);//状态1是起售
            return dishMapper.selectList(qw);


    }


    // app菜品展示
    @Cacheable(value = "Dish",key = "#categoryId")
    @Override
    public List<Dish> List(Long categoryId, Integer status) {
        log.info("--------根据分类查询列表(存redis)--------");
        //构建条件,查询菜品列表
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId, categoryId);
        qw.eq(Dish::getStatus, status);
        List<Dish> dishList = dishMapper.selectList(qw);
        //遍历菜品
        for (Dish dish : dishList) {
            //根据菜品查询口味
            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw2);
            //把口味封装到菜品中
            dish.setFlavors(dishFlavors);
        }
        //返回结果
        return dishList;
    }


    //根据条件菜品列表(回显)
    @Override
    public List<Dish> findByname(String name) {
        //根据name模糊查询菜品(前端写死了name为空进不来,后端不用非空判断了)
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(Dish::getName,name);
        qw.eq(Dish::getStatus,1);
        List<Dish> dishList = dishMapper.selectList(qw);
        return dishList;
    }
}
